﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccountManager
{
    public partial class Form1 : Form
    {
        Manager manager = new Manager();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshList();
            RefreshTree();
        }

        private void RefreshList()
        {
            listView_const_value.Items.Clear();
            foreach (var item in manager.GetConstants())
                listView_const_value.Items.Add(new ListViewItem(new string[] { item.Value.Key, item.Value.Value }));
        }

        private void RefreshTree()
        {
            treeView1.Nodes.Clear();
            foreach (var item in manager.GetAccounts())
            {
                TreeNode[] m_treeNodes = new TreeNode[item.Value.Contents.Count];
                for(int i = 0; i < item.Value.Contents.Count; ++i)
                    m_treeNodes[i] = new TreeNode(item.Value.Contents[i].UseConstant ? manager.GetConstants()[item.Value.Contents[i].Value].Value : item.Value.Contents[i].Value);
                treeView1.Nodes.Add(new TreeNode(item.Value.Key,m_treeNodes));
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            manager.AddConstant(textBox_key.Text, textBox_value.Text);
            RefreshList();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView_const_value.SelectedItems)
            {
                manager.GetConstants().Remove(item.Text);
            }
            RefreshList();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //treeView1.Nodes.Add(textBox_tag.Text);
            manager.AddAccount(new Account() { Key = textBox_tag.Text });
            RefreshTree();
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null) return;
            if(checkBox1.Checked && !manager.GetConstants().ContainsKey(textBox_content_value.Text))
            {
                MessageBox.Show("Can't find a const refered to this");
                return;
            }
            if (treeView1.SelectedNode.Parent == null)
            {
                manager.GetAccounts()[treeView1.SelectedNode.Text].Contents.Add(new Content() { UseConstant = checkBox1.Checked, Value = textBox_content_value.Text });
                RefreshTree();
            }else
            {
                MessageBox.Show("You should add value to a root node");
            }
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            Clipboard.SetText(treeView1.SelectedNode.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null) return;
            if (treeView1.SelectedNode.Parent == null)
            {
                manager.GetAccounts().Remove(treeView1.SelectedNode.Text);
            }
            else
            {
                var tmp = manager.GetAccounts()[treeView1.SelectedNode.Parent.Text].Contents;
                for (int i = 0; i < tmp.Count; ++i)
                {
                    if((tmp[i].UseConstant && (manager.GetConstants()[tmp[i].Value].Value == treeView1.SelectedNode.Text)) || (!tmp[i].UseConstant && (tmp[i].Value == treeView1.SelectedNode.Text)))
                    {
                        tmp.RemoveAt(i);
                        break;
                    }
                }
            }
            RefreshTree();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode == null) return;
            panel1.Enabled = (treeView1.SelectedNode.Parent == null);
        }

        private void listView_const_value_DoubleClick(object sender, EventArgs e)
        {
            Clipboard.SetText(listView_const_value.SelectedItems[0].Text);
        }
    }
}
